package com.ahstu.learning.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ahstu.learning.test.ErrorQuestionInfo;

/**
 * 数据库操作类
 */
public class DBManager {

    private Context context;
    private SQLiteDatabase database;

    public DBManager(Context context) {
        this.context = context;
    }

    /**
     * 打开数据库，如果不存在则创建一个数据库
     */
    public void openDB() {
        DBHelper dbHelper = new DBHelper(context);
        if (database == null || !database.isOpen()) {
            database = dbHelper.getWritableDatabase();
        }
    }

    /**
     * 关闭数据库
     */
    public void closeDB() {
        if (database != null && database.isOpen()) {
            database.close();
        }
    }

    /**
     * 删除我的题库所有数据
     * @return
     */
    public long deleteLibraryAllData() {
        return database.delete(DBHelper.TABLE_NAME_TEST_LIBRARY, null,
                null);
    }

    /**
     * 添加一条我的错题数据
     *
     * @param info
     * @return
     */
    public long insertErrorQuestion(ErrorQuestionInfo info) {

        ContentValues newValues = new ContentValues();

        newValues.put(DBHelper.MY_ERROR_QUESTION_NAME, info.getQuestionName());
        newValues.put(DBHelper.MY_ERROR_QUESTION_TYPE, info.getQuestionType());
        newValues.put(DBHelper.MY_ERROR_QUESTION_ANSWER, info.getQuestionAnswer());
        newValues.put(DBHelper.MY_ERROR_QUESTION_SELECTED, info.getQuestionSelect());
        newValues.put(DBHelper.MY_ERROR_QUESTION_ISRIGHT, info.getIsRight());
        newValues.put(DBHelper.MY_ERROR_QUESTION_ANALYSIS, info.getAnalysis());
        newValues.put(DBHelper.MY_ERROR_QUESTION_OPTION_A, info.getOptionA());
        newValues.put(DBHelper.MY_ERROR_QUESTION_OPTION_B, info.getOptionB());
        newValues.put(DBHelper.MY_ERROR_QUESTION_OPTION_C, info.getOptionC());
        newValues.put(DBHelper.MY_ERROR_QUESTION_OPTION_D, info.getOptionD());
        newValues.put(DBHelper.MY_ERROR_QUESTION_OPTION_TYPE, info.getOptionType());

        return database.insert(DBHelper.TABLE_NAME_MY_ERROR_QUESTION, null,
                newValues);
    }

    /**
     * 查询错题数据是否存在
     * @param info
     * @return
     */
    public boolean searchErrorQuestion(ErrorQuestionInfo info){
        Cursor cursor=database.query(DBHelper.TABLE_NAME_MY_ERROR_QUESTION,new String[]{DBHelper.MY_ERROR_QUESTION_NAME},
                DBHelper.MY_ERROR_QUESTION_NAME+" like ?",new String[]{info.getQuestionName()},null,null,null);
        if (cursor.getCount()>0)
            return true;
        return false;
    }

    /**
     * 删除我的错题所有数据
     *
     * @return
     */
    public long deleteAllData() {
        return database.delete(DBHelper.TABLE_NAME_MY_ERROR_QUESTION, null,
                null);
    }

    /**
     * 查询全部我的错题数据
     *
     * @return
     */
    public ErrorQuestionInfo[] queryAllData() {
//        Cursor result=database.rawQuery("select * from "+DBHelper.TABLE_NAME_MY_ERROR_QUESTION+" where "
//                +DBHelper.MY_ERROR_QUESTION_TYPE+" like "+type,null);
        Cursor result = database.query(DBHelper.TABLE_NAME_MY_ERROR_QUESTION,null,
                null,null,null,null,null);

        return ConvertToQuestion(result);
    }

    /**
     * ConvertToPeople(Cursor cursor)是私有函数， 作用是将查询结果转换为用来存储数据自定义的People类对象
     * People类的包含四个公共属性，分别为ID、Name、Age和Height，对应数据库中的四个属性值
     */
    private ErrorQuestionInfo[] ConvertToQuestion(Cursor cursor) {
        int resultCounts = cursor.getCount();
        if (resultCounts == 0 || !cursor.moveToFirst()) {
            return null;
        }
        ErrorQuestionInfo[] errorQuestion = new ErrorQuestionInfo[resultCounts];
        for (int i = 0; i < resultCounts; i++) {
            errorQuestion[i] = new ErrorQuestionInfo();
            errorQuestion[i].questionId = cursor.getInt(0);
            errorQuestion[i].questionName = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_NAME));
            errorQuestion[i].questionType = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_TYPE));
            errorQuestion[i].questionAnswer = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_ANSWER));
            errorQuestion[i].questionSelect = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_SELECTED));
            errorQuestion[i].isRight = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_ISRIGHT));
            errorQuestion[i].Analysis = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_ANALYSIS));
            errorQuestion[i].optionA = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_OPTION_A));
            errorQuestion[i].optionB = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_OPTION_B));
            errorQuestion[i].optionC = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_OPTION_C));
            errorQuestion[i].optionD = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_OPTION_D));
            errorQuestion[i].optionType = cursor.getString(cursor
                    .getColumnIndex(DBHelper.MY_ERROR_QUESTION_OPTION_TYPE));
            cursor.moveToNext();
        }
        return errorQuestion;
    }

}
